// 新建用户 
// mixNick: mixNick,
// isFirst: isFirst,
// itemId: params.itemId,
// num: itemIdArrayCopy.length

exports.createOne = async (params, cloud) => {
  try {
    //先判断是否存在当前主用户ids
    let sql = `INSERT INTO shopping (gmt_create,gmt_modified,mixNick,itemId,isFirst,num)
     VALUES(now(),now(),'${params.mixNick}','${params.itemId}','${params.isFirst}','${params.num}');`
     console.log('"==========sql===========',sql)
    let data = await cloudfn(cloud, sql);
    return data;
  } catch (err) {
    return err;
  }
}


/**
 * 查询最后一次加购id
 */
exports.findLastAddCartId = async (mixNick, cloud) => {
  try {
      let sql = `select * from shopping where mixnick = '${mixNick}' AND  gmt_create > '2020-11-04 00:00:00'`;
        //  let sql = `select * from shopping where mixnick = '${mixNick}' order by gmt_create desc limit 0,1`;
    let data = await cloudfn(cloud, sql);
    return data[0];
  } catch (err) {
    return err;
  }
};





/**
 * 查询符合中奖资格的用户
 */
exports.findWinning = async (cloud) => {
  try {
    let sql = `SELECT mixNick FROM shopping where  gmt_create > '2020-11-04 00:00:00' AND num < 4 `;
    let data = await cloudfn(cloud, sql);
    return data;
  } catch (err) {
    return err;
  }
};



cloudfn = async (cloud, sql) => {
  console.log("==============123sql==================", sql)
  let cbData = await cloud.dataspace.executeSql(sql, [])
  console.log("==============cbData==================", cbData)
  return cbData
}




